class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        if(head == NULL || head->next == NULL) return NULL;
        ListNode* fast = head->next->next;
        ListNode* slow = head->next;
        while(fast && fast->next && fast != slow){
            fast = fast->next->next;
            slow = slow->next;
        }
        slow = head;
        while(fast && fast != slow){
            fast = fast->next;
            slow = slow->next;
        }
        return fast;
    }
};
